//
//  GFCollectView.m
//  Qyer
//
//  Created by dlios on 15/10/22.
//  Copyright (c) 2015年 GF. All rights reserved.
//

#import "GFCollectView.h"

@implementation GFCollectView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        _itemArray = [NSMutableArray array];
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        [flowLayout setMinimumInteritemSpacing:WIDTH * 10.];
        [flowLayout setMinimumLineSpacing:WIDTH * 10];
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        [flowLayout setItemSize:CGSizeMake((SCREEN_WIDTH - 30 * WIDTH) / 2, 230 * HEIGHT)];
        [flowLayout setSectionInset:UIEdgeInsetsMake(5 * HEIGHT, 10 * WIDTH, 5 * HEIGHT, 10 * WIDTH)];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 162 * HEIGHT) collectionViewLayout:flowLayout];
        [_collectionView setDelegate:self];
        [_collectionView setDataSource:self];
        [_collectionView registerClass:[GFDetailCollectionViewCell class] forCellWithReuseIdentifier:@"item"];
        [_collectionView setBackgroundColor:[UIColor whiteColor]];
        [self addSubview:_collectionView];
        [self getAfnString];
//        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionElementKindSectionHeader"];
        
    }
    return self;
}


//#pragma mark-返回增补视图 增加头部
//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
//    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
//        _sectionArray = [NSMutableArray arrayWithObjects:@"亚洲", @"欧洲", @"美洲、大洋洲、非洲与南极洲", @"刚港澳台", @"大陆", nil];
//       UICollectionReusableView *headerView= [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"UICollectionElementKindSectionHeader" forIndexPath:indexPath];
//        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30 * HEIGHT)];
//        [lable setText:[_sectionArray objectAtIndex:indexPath.section]];
//        [lable setFont:[UIFont systemFontOfSize:WIDTH * 15.]];
//        [lable setTextColor:[NightMode shareNightModeHandle].wordColor];
//        [headerView addSubview:lable];
//                return headerView;
//    }
//    return nil;
//}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(375 * WIDTH, 30 * HEIGHT);
}
#pragma mark-通过消息中心将id传值,拼接字符串进行网路请求并赋值
- (void)getAfnString{
    [_itemArray removeAllObjects];
    [_sectionArray removeAllObjects];
    NSString *comString = @"http://chanyouji.com/api/destinations.json";
    [AFN getdataWithString:comString block:^(id block) {
        for (NSDictionary *dic in block) {
            GFJnModel *model = [GFJnModel modelWithDictionary:dic];
            [_sectionArray addObject:model];
            for (NSDictionary *iDic in [dic objectForKey:@"destinations"]) {
                GFJnModel *itemModel = [GFJnModel modelWithDictionary:iDic];
                [_itemArray addObject:itemModel];
            }
        }
        [_collectionView reloadData];
    } BLOCK:^(id BLOCK) {
        
    }];
    
}

//- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
//    return _sectionArray.count;
//}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _itemArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GFDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    [cell setBackgroundColor:[NightMode shareNightModeHandle].allViewColor];
    GFJnModel *model = [_itemArray objectAtIndex:indexPath.row];
    [cell setJnModel: model];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    GFJnModel *model = [_itemArray objectAtIndex:indexPath.row];
    [self.cDelegate getGFJnModel:model];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
